package com.shenglaotuan.community.tag.controller;


import com.shenglaotuan.community.common.entity.JwtConfig;
import com.shenglaotuan.community.common.entity.ResponseResult;
import com.shenglaotuan.community.common.utils.TokenUtils;
import com.shenglaotuan.community.tag.entity.Tag;
import com.shenglaotuan.community.tag.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ruidong
 * @since 2020-08-22
 */
@Validated
@Api(tags = "文章标签模块")
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @Autowired
    private JwtConfig jwtConfig;

    @ApiOperation("添加标签")
    @PostMapping("/add")
    public ResponseResult<List<String>> addTag(@NotNull(message = "标签不能为空") String[] tags, HttpServletRequest request){
        String uid = TokenUtils.getIdByRequest(request, jwtConfig);
        List<String> data = tagService.addTag(uid, tags);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("删除标签")
    @PostMapping("/del")
    public ResponseResult<Boolean> delTag(String id){
        boolean data = tagService.removeById(id);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("获取标签")
    @GetMapping("/list")
    public ResponseResult<List<Tag>> getTags(@NotNull(message = "获取标签失败") String uid){
        List<Tag> data = tagService.listUserTag(uid);
        return ResponseResult.responseSuccess(data);
    }
}

